package com.qzq.weibo.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.qzq.weibo.R;
import com.qzq.weibo.adapter.UsersAdapter;
import com.qzq.weibo.entity.User;
import com.qzq.weibo.http.HttpRequest;
import com.qzq.weibo.utils.Utils;
import com.qzq.weibo.view.LoadMoreListView;
import com.qzq.weibo.view.LoadMoreListView.OnLoadMoreListener;
import com.qzq.weibo.view.LoadingDialog;

public class UserFriendsActivity extends Activity {

	private LoadMoreListView lv_friends;
	private ImageView iv_back;

	private LoadingDialog dialog_loading;

	private List<User> friends;
	private UsersAdapter adapter;

	private int count = 10;
	private int cursor = 0;

	private HttpRequest http;
	private Utils utils;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_friends);

		initView();
		initMember();
		loadFriends();
	}

	private void initView() {
		lv_friends = (LoadMoreListView) this.findViewById(R.id.user_friends_lv);
		iv_back = (ImageView) this.findViewById(R.id.user_friends_bar_back);

	}

	private void initMember() {

		utils = Utils.getInstance(getApplicationContext());
		http = HttpRequest.getInstance(getApplicationContext());
		friends = new ArrayList<User>();

		adapter = new UsersAdapter(getApplicationContext(),
				R.layout.list_item_user, friends);
		lv_friends.setAdapter(adapter);
		lv_friends.setOnLoadMoreListener(loadMoreListener);

		iv_back.setOnClickListener(onClickBackListener);

	}

	/** iv_back按键监听 */
	private OnClickListener onClickBackListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	/** 加载用户关注列表 */
	@SuppressLint("HandlerLeak")
	private void loadFriends() {

		dialog_loading = new LoadingDialog(UserFriendsActivity.this,
				R.style.Theme_Transparent, "数据加载中...");

		http.requestGetFriends(new Handler() {
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (msg != null) {
					dialog_loading.dismiss();
					int flag = msg.what;
					if (flag == HttpRequest.REQUEST_FAILER) {
						HttpRequest.badRequest(
								Integer.parseInt(msg.obj.toString()),
								getApplicationContext());
					} else {
						friends.addAll((Collection<? extends User>) msg.obj);
						adapter.notifyDataSetChanged();
					}
				}
			}
		}, cursor, count);

	}

	/** 下拉加载更多 */
	private OnLoadMoreListener loadMoreListener = new OnLoadMoreListener() {

		@SuppressLint("HandlerLeak")
		@Override
		public void onLoadMore() {
			cursor += 10;
			http.requestGetFriends(new Handler() {

				@SuppressWarnings("unchecked")
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg != null) {
						lv_friends.onLoadMoreComplete();
						int flag = msg.what;
						if (flag == HttpRequest.REQUEST_FAILER) {
							HttpRequest.badRequest(
									Integer.parseInt(msg.obj.toString()),
									getApplicationContext());
						} else {
							List<User> temp = (List<User>) msg.obj;
							if (temp.size() != 0) {
								friends.addAll(temp);
								adapter.notifyDataSetChanged();
							} else {
								utils.setToastMsg("没有数据了");
							}
						}
					}
				}
			}, cursor, count);

		}
	};
}
